/*
 * The MIT License
 * 
 * Copyright (c) 2008 Comcast Interactive Media
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package cim.sf.core.command.internal;

import java.io.PrintStream;

import org.osgi.framework.Bundle;

import cim.sf.core.command.Command;
import cim.sf.core.command.CommandInvocation;
import cim.sf.core.command.InvalidUsageException;

/**
 * @author <a href="mailto:jeremy_volkman@cable.comcast.com">Jeremy Volkman</a>
 */
public class CommandUtils {
    
    public static int executeCommand(Command cmd, Bundle b, CommandInvocation ci) {
        if (cmd != null) {
            // Special case for '--help' option
            if (ci.args.length > 0 && "--help".equals(ci.args[0])) {
                printHelp(cmd, b, ci.out);
                return 0;
            } else {
                try {
                    return cmd.execute(ci);
                } catch (InvalidUsageException e) {
                    invalidUsage(cmd, ci.out, e.getMessage());
                    return Command.ERROR_USAGE;
                } catch (Throwable t) {
                    if (t instanceof ThreadDeath) {
                        throw ((ThreadDeath)t);
                    }
                    ci.err.println("Error occured while executing command");
                    t.printStackTrace(ci.err);
                    return Command.ERROR_GENERAL;
                }
            }
        } else {
            ci.err.println("ERROR: Command not found");
            return Command.ERROR_GENERAL;
        }
    }
    
    private static void printHelp(Command cmd, Bundle b, PrintStream out) {
        printUsage(cmd, out);
        if (cmd.getDescription() != null) {
            out.println("DESCRIPTION");
            out.println("\t" + cmd.getDescription());
        }
        if (b != null) {
            out.println("BUNDLE");
            out.println("\t" + b.getSymbolicName());
        }
    }
    
    private static void invalidUsage(Command cmd, PrintStream out, String msg) {
        if (msg == null) {
            out.println("Invalid command usage");
        } else {
            out.println("Invalid command usage: " + msg);
        }
        printUsage(cmd, out);
    }
    
    private static void printUsage(Command cmd, PrintStream out) {
        if (cmd.getUsage() != null && cmd.getUsage().length > 0) {
            out.println("USAGE");
            for (String usage : cmd.getUsage()) {
                out.println("\t" + usage);
            }
        }
    }
}
